
from langchain.chat_models import ChatOpenAI

import langchain

from config.model_config import get_chat_openai_xin

print(langchain.__version__)

def get_weather(city: str):
    """模拟天气API返回数据"""
    weather_data = {
        "上海": {"temp": 25, "condition": "多云"},
        "北京": {"temp": 30, "condition": "晴"}
    }
    return weather_data.get(city, {"error": "城市不存在"})


# 定义工具结构（JSON Schema格式）
tools = [{
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "获取指定城市的天气信息",
            "parameters": {
                "type": "object",
                "properties": {"city": {"type": "string"}},
                "required": ["city"]
            }
        }
    }]

model = get_chat_openai_xin(streaming=False)

model_with_tools = model.bind_tools(tools)

# 调用工具
response = model_with_tools.invoke("上海现在天气怎么样")
# content='' additional_kwargs={'tool_calls': [{'id': 'call_6d6d001d-031c-41dc-9d1c-000bbc7f3028', 'function': {'arguments': '{"city": "上海"}', 'name': 'get_weather'},
# 'type': 'function'}], 'refusal': None} response_metadata={'token_usage': {'completion_tokens': 19, 'prompt_tokens': 166, 'total_tokens': 185, 'completion_tokens_details': None,
# 'prompt_tokens_details': None}, 'model_name': 'Qwen2.5-0.5B-Instruct', 'system_fingerprint': None, 'id': 'chatcmpl-6d6d001d-031c-41dc-9d1c-000bbc7f3028', 'service_tier': None,
# 'finish_reason': 'tool_calls', 'logprobs': None} id='run--f30260f5-f216-40de-9bc0-2f9883525679-0'
# tool_calls=[{'name': 'get_weather', 'args': {'city': '上海'}, 'id': 'call_6d6d001d-031c-41dc-9d1c-000bbc7f3028', 'type': 'tool_call'}]
# usage_metadata={'input_tokens': 166, 'output_tokens': 19, 'total_tokens': 185, 'input_token_details': {}, 'output_token_details': {}}
print(response)

# 失败
